﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DataAccess;

namespace QUANLYKHACHSAN_N4
{
    public partial class frmQuanLyNhanVien : DevComponents.DotNetBar.Office2007RibbonForm
    {
        public frmQuanLyNhanVien()
        {
            InitializeComponent();
        }
        private void frmQuanLyNhanVien_Load(object sender, EventArgs e)
        {
            tabAll_Click(null, null);
            cbChucVu.DataSource = ChucVu_Info.All().Tables[0];
            cbChucVu.DisplayMember = "chucvu";
            cbGioiTinh.SelectedIndex = cbChoose.SelectedIndex = 0;
            XuLyDataGridView.Style(dgrDanhSach);
        }
        private void tabAll_Click(object sender, EventArgs e)
        {
            TabConTrol.SelectedTab = tabAll;
            dgrDanhSach.DataSource = NhanVien_Info.GetAll().Tables[0];
            tabView.Visible = false;
            TabConTrol.Height = 26;
        }
        private void tabThem_Click(object sender, EventArgs e)
        {
            txtMaNV.Enabled = true;
            tabView.Visible = false;
            TabConTrol.Height = 159;
            this.tabThem.AttachedControl = this.tabpnInfo;
            btnLamLai_Click(null, null);
            btnSua.Enabled = btnXoa.Enabled = false;
            btnThem.Enabled = true;
        }
        private void tabTimKiem_Click(object sender, EventArgs e)
        {
            tabView.Visible = false;
            TabConTrol.Height = 100;
            switch (cbChoose.SelectedIndex)
            {
                case 0:
                    dgrDanhSach.DataSource = NhanVien_Info.GetByName(txtSearch.Text);
                    break;
                case 1:
                    dgrDanhSach.DataSource = NhanVien_Info.GetByID(txtSearch.Text);
                    break;
                case 2:
                    dgrDanhSach.DataSource = NhanVien_Info.GetByCMND(txtSearch.Text);
                    break;
            }

        }
        private void tabView_Click(object sender, EventArgs e)
        {
            this.tabView.AttachedControl = this.tabpnInfo;
            tabView.Visible = true;
            TabConTrol.SelectedTab = tabView;
            TabConTrol.Height = 159;
            btnSua.Enabled = btnXoa.Enabled = true;
            btnThem.Enabled = false;
        }
        void BinGidToForm(DataGridViewRow r)
        {
            try
            {
                txtMaNV.Text = r.Cells["manv"].Value.ToString();
                txtHoTen.Text = r.Cells["hovaten"].Value.ToString();
                txtDiaChi.Text = r.Cells["diachi"].Value.ToString();
                txtDienThoai.Text = r.Cells["dienthoai"].Value.ToString();
                txtCMND.Text = r.Cells["cmnd"].Value.ToString();
                cbChucVu.Text = r.Cells["chucvu"].Value.ToString();
                cbGioiTinh.Text = Convert.ToBoolean(r.Cells["gioitinh"].Value) ? "Nam" : "Nữ";
                dtpNgaySinh.Value = Convert.ToDateTime(r.Cells["gioitinh"].Value);
            }
            catch (Exception)
            {
            }
        }
        private void btnLamLai_Click(object sender, EventArgs e)
        {
            try
            {

                if (!tabView.Visible)
                {
                    txtMaNV.Text =
                    txtHoTen.Text =
                    txtDiaChi.Text =
                    txtDienThoai.Text =
                    txtCMND.Text = "";
                    DuLieu.GetValueInCombo("", cbChucVu);
                    DuLieu.GetGioiTinh("", cbGioiTinh);
                    dtpNgaySinh.Value = DateTime.Now;
                }
                else
                    BinGidToForm(dgrDanhSach.CurrentRow);
            }
            catch (Exception)
            {
            }
        }
        private void dgrDanhSach_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            try
            {
                txtMaNV.Enabled = false;
                BinGidToForm(dgrDanhSach.CurrentRow);
                tabView_Click(null, null);
            }
            catch (Exception)
            {
            }
        }
        private void btnThem_Click(object sender, EventArgs e)
        {
            try
            {
                if (txtMaNV.Text == "")
                    BatLoi.DgResult("chua nhap ma nhan vien ");
                else
                    if (txtHoTen.Text == "")
                        BatLoi.DgResult("chua nhap ho ten");
                    else
                        if (txtCMND.Text == "")
                            BatLoi.DgResult("chua nhap so CMND");
                        else
                            if (txtDiaChi.Text == "")
                                BatLoi.DgResult("chua nhap dia chi");
                            else
                                if (txtDienThoai.Text == "")
                                    BatLoi.DgResult("chua nhap dien thoai");
                                

                                else
                                {
                                    bool kt1 = true;
                                    try
                                    {

                                        int scmd = Convert.ToInt32(txtCMND.Text);
                                    }
                                    catch
                                    {
                                        kt1 = false;
                                    }
                                    if (kt1 == false)
                                        MessageBox.Show("Du Lieu Nhap Vao O  CMND Chua Dung", "Thong Bao", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                    else if (txtCMND.Text.Length != 9)
                                        MessageBox.Show("Vui Long Kiem Tra So CMND", "Thong Bao", MessageBoxButtons.OK, MessageBoxIcon.Information);

                                    else
                                    {
                                        if (NhanVien_Info.Add(txtMaNV.Text, txtHoTen.Text, dtpNgaySinh.Value, txtDiaChi.Text, txtDienThoai.Text, int.Parse(txtCMND.Text), cbGioiTinh.Text == "Nam", cbChucVu.Text))
                                        {
                                            BatLoi.ThongBao("Thêm Nhân Viên Thành Công");
                                            tabAll_Click(null, null);

                                        }
                                    }
                                }
            }
            catch
            { }
           

        }
        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (NhanVien_Info.Delete(txtMaNV.Text))
            {
                BatLoi.ThongBao("Xóa Nhân Viên Thành Công");
                tabAll_Click(null, null);
            }
        }
        private void btnSua_Click(object sender, EventArgs e)
        {
            try
            {
                if (txtMaNV.Text == "")
                    BatLoi.DgResult("chua nhap ma nhan vien ");
                else
                    if (txtHoTen.Text == "")
                        BatLoi.DgResult("chua nhap ho ten");
                    else
                        if (txtCMND.Text == "")
                            BatLoi.DgResult("chua nhap so CMND");
                        else
                            if (txtDiaChi.Text == "")
                                BatLoi.DgResult("chua nhap dia chi");
                            else
                                if (txtDienThoai.Text == "")
                                    BatLoi.DgResult("chua nhap dien thoai");
                                

                                else
                                {
                                    bool kt1 = true;
                                    try
                                    {

                                        int scmd = Convert.ToInt32(txtCMND.Text);
                                    }
                                    catch
                                    {
                                        kt1 = false;
                                    }
                                    if (kt1 == false)
                                        MessageBox.Show("Du Lieu Nhap Vao O  CMND Chua Dung", "Thong Bao", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                    else if (txtCMND.Text.Length != 9)
                                        MessageBox.Show("Vui Long Kiem Tra So CMND", "Thong Bao", MessageBoxButtons.OK, MessageBoxIcon.Information);

                                    else
                                    {

                                        if (NhanVien_Info.Update(txtMaNV.Text, txtHoTen.Text, dtpNgaySinh.Value, txtDiaChi.Text, txtDienThoai.Text, int.Parse(txtCMND.Text), cbGioiTinh.Text == "Nam", cbChucVu.Text))
                                        {
                                            BatLoi.ThongBao("Update Nhân Viên Thành Công");
                                            tabAll_Click(null, null);
                                        }

                                        }
                                    }
            }
            catch
            { }



        }
            //if (NhanVien_Info.Update(txtMaNV.Text, txtHoTen.Text, dtpNgaySinh.Value, txtDiaChi.Text, txtDienThoai.Text, int.Parse(txtCMND.Text), cbGioiTinh.Text == "Nam", cbChucVu.Text))
            //{
            //    BatLoi.ThongBao("Update Nhân Viên Thành Công");
            //    tabAll_Click(null, null);
            //}


        private void btnXoaSelectItem_Click(object sender, EventArgs e)
        {
            if (BatLoi.DgResult("Bạn có muốn xóa những nhân viên được chọn"))
            {
                foreach (DataGridViewRow r in dgrDanhSach.SelectedRows)
                    NhanVien_Info.Delete(r.Cells["manv"].Value.ToString());
            }
        }

        private void btnInAn_Click(object sender, EventArgs e)
        {
            try
            {

                frmFormMain frm = MdiParent as frmFormMain;
                frm.pr = new frmPrint();
                DuLieuRP.SetKhungPrint(frm.pr, "ds_NhanVien_GetID", frm.pr.NhanVien_GetByIDBindingSource);
                frm.pr.rpViewer.LocalReport.ReportEmbeddedResource = "QUANLYKHACHSAN_N4.REPORT.rpThongTinNhanVien.rdlc";
                frm.pr.NhanVien_GetByIDBindingSource.DataSource = NhanVien_Info.GetByMaSo(txtMaNV.Text).Tables[0];
                frm.pr.rpViewer.RefreshReport();
                frm.pr.Text = "THÔNG TIN NHÂN VIÊN " + txtHoTen.Text;
                frm.ShowPrint();
            }
            catch (Exception)
            {

            }

        }
    }
}
